package logic

import (
	"cloud-disk/core/internal/svc"
	"cloud-disk/core/internal/types"
	"cloud-disk/core/models"
	"context"
	"errors"

	"github.com/zeromicro/go-zero/core/logx"
)

type UserFileMoveLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewUserFileMoveLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UserFileMoveLogic {
	return &UserFileMoveLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *UserFileMoveLogic) UserFileMove(req *types.UserFileMoveRequest, UserIdentity string) (resp *types.UserFileMoveResponse, err error) {
	//查看父文件夹是否存在
	var has int64
	var ur models.UserRepository
	rows, err := l.svcCtx.DbConn.Table("user_repository").Where("identity=? AND user_identity=?", req.ParentIdentity, UserIdentity).First(&ur).Rows()
	defer rows.Close()
	if err != nil {
		return nil, err
	}
	if rows.Next() {
		has++
	}
	if has <= 0 {
		return nil, errors.New("文件夹不存在")
	}
	//父文件夹是否是自己
	if ur.Identity == req.Identity {
		return nil, errors.New("文件夹不合法")
	}
	err = l.svcCtx.DbConn.Table("user_repository").Where("identity=?", req.Identity).Update("parent_id", int64(ur.ID)).Error
	if err != nil {
		return
	}
	return
}
